package com.deyang.ui.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.deyang.adapter.OrganizationAdapter;
import com.deyang.callback.ValueCallBack;
import com.deyang.config.HttpConfig;
import com.deyang.config.IntentConfig;
import com.deyang.entity.OrganizationModel;
import com.deyang.maillist.R;
import com.deyang.service.IHttpService;
import com.deyang.tool.HttpTool;
import com.deyang.ui.activity.MailListActivity;
import com.deyang.ui.activity.SearchMailListActivity;
import com.deyang.util.DialogUtil;
import com.deyang.util.RetrofitUtil;
import com.deyang.util.ViewUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author wangdyang
 * @createDate 2018年7月18日10:53:31
 * @desc 首页
 */
public class HomePageFragment extends BaseFragment {


    private IHttpService httpService;


    @BindView(R.id.tv_noData)
    TextView tvNoData;
    @BindView(R.id.rv_organization)
    RecyclerView rvOrganization;


    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    private List<OrganizationModel> organizationModelList = new ArrayList<>();
    private OrganizationAdapter organizationAdapter;

    private int currentPage = 1, maxPage = 5;

    private String[] organizationNames = {"总经办", "行政部", "财务部", "市场部", "工程部", "采购部", "设计部", "研发部", "营销部", "运输管理部"};


    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_home_page;
    }

    @Override
    protected void processLogic() {
        httpService = RetrofitUtil.buildRetrofit(mActivity).create(IHttpService.class);
        organizationAdapter = new OrganizationAdapter(mActivity, organizationModelList);
        ViewUtil.initRecycleView(mActivity, false, rvOrganization, organizationAdapter);
        loadData(false);

        setListener();
    }


    public void setListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                loadData(false);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadData(true);
            }
        });

        organizationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                OrganizationModel organizationModel = organizationModelList.get(position);
                Intent intent = new Intent(mActivity, MailListActivity.class);
                intent.putExtra("organizationName", organizationModel.getName());
                intent.putExtra("organizationNumber", organizationModel.getNumber());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }


    /**
     * 加载数据
     *
     * @param isLoadMore
     */
    private void loadData(final boolean isLoadMore) {
        if (!isLoadMore) {
            currentPage = 1;
        } else {
            currentPage++;
        }
        if (currentPage > maxPage) {
            showToastError("无更多数据");
            refreshLayout.finishLoadMore();
            return;
        }
        Dialog loadingDialog = DialogUtil.getLoadingDialog(mActivity);
        loadingDialog.show();

        List<OrganizationModel> newOrganizationModelList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            newOrganizationModelList.add(new OrganizationModel(organizationNames[i], (20 * i + 1) + ""));
        }

        if (!isLoadMore) {
            organizationModelList.clear();
            organizationAdapter.setNewData(newOrganizationModelList);
            if (newOrganizationModelList.size() == 0) {
                tvNoData.setVisibility(View.VISIBLE);
            } else {
                tvNoData.setVisibility(View.GONE);
            }
        } else {
            organizationAdapter.addData(newOrganizationModelList);
        }
        organizationModelList.addAll(newOrganizationModelList);
        if (isLoadMore) {
            refreshLayout.finishLoadMore();
        } else {
            refreshLayout.finishRefresh();
        }
        loadingDialog.dismiss();
    }


    @OnClick({R.id.ll_search})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_search:
                Intent intent = new Intent(mActivity, SearchMailListActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


}



















